const {User} = require('../../model/user')

module.exports = async (req,res)=>{

    const {username,email,role,state} = req.body
    const id = req.query.id

    if(username.trim().length == 0 || email.trim().length == 0){
        return res.render('admin/user-edit',{
             username:req.session.username,
             message:'参数错误：参数不能为空'
         })
     }
      let resObj = await User.findOne({email})

      if(resObj && resObj._id != id){
          return res.render('admin/user-edit',{
            message:'参数email错误：邮箱已注册',
            username:req.session.username,
            userID:id,
            user:{
                username,
                email,
                role,
                state
            },
            url:'/admin/user-edit?id='+id
        })
      }
    let result = await User.updateOne({_id:id},{username,email,role,state})
    res.redirect('/admin/user')
}